﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_friendlink_friendlink_edit : System.Web.UI.Page
{
    FriendLinkEntity friendLinkEntity = new FriendLinkEntity();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.hfdFlag.Value = string.IsNullOrEmpty(Request.QueryString["flag"]) ? "" : Request.QueryString["flag"];
            this.InitControl();
        }
    }

    /// <summary>
    /// 页面初始化
    /// </summary>
    private void init()
    {
        int linkID = string.IsNullOrEmpty(Request.QueryString["linkID"]) ? 0 : int.Parse(Request.QueryString["linkID"]);
        if (linkID != 0)
        {
            this.friendLinkEntity = DB.GetModel(linkID);
            this.txtLinkName.Text = this.friendLinkEntity.linkName;
            this.txtUrl.Text = this.friendLinkEntity.linkUrl;
            this.chkDisplay.Checked = this.friendLinkEntity.linkDisplay;
            this.txtDesc.Text = this.friendLinkEntity.linkDesc;
            this.txtSort.Text = this.friendLinkEntity.linkSort.ToString();
            this.txtAddDate.Text = this.friendLinkEntity.linkAddDate.ToString();
            this.hfdID.Value = this.friendLinkEntity.linkID.ToString();
        }
    }

    /// <summary>
    /// 页面获取控件值
    /// </summary>
    private void GetModelFromPage()
    {
        this.friendLinkEntity.linkName = this.txtLinkName.Text;
        this.friendLinkEntity.linkUrl = this.txtUrl.Text;
        this.friendLinkEntity.linkDisplay = this.chkDisplay.Checked;
        this.friendLinkEntity.linkDesc = this.txtDesc.Text;
        this.friendLinkEntity.linkSort = int.Parse(this.txtSort.Text);
        this.friendLinkEntity.linkAddDate = DateTime.Now;
        if (!string.IsNullOrEmpty(this.hfdID.Value))
            this.friendLinkEntity.linkID = int.Parse(this.hfdID.Value);
    }

    /// <summary>
    /// 页面控件设置
    /// </summary>
    private void InitControl()
    {
        if ("look" == this.hfdFlag.Value)//查看页面
        {
            this.init();
            this.imgBtn.Visible = false;
            foreach (Control control in this.form1.Controls)
            {
                if (control is TextBox)
                {
                    ((TextBox)control).Enabled = false;
                }
            }
            this.chkDisplay.Enabled = false;
            this.message.Msg = "友情链接（查看）";
            this.message.msgIco = xu_happy_you.Control.Message.msgIcos.information;
        }
        else if ("edit" == this.hfdFlag.Value)//页面编辑
        {
            this.init();
            this.imgBtn.Visible = true;
            this.txtAddDate.Enabled = false;
            this.message.Msg = "友情链接（编辑）";
            this.message.msgIco = xu_happy_you.Control.Message.msgIcos.information;
        }
        else//新增
        {
            this.imgBtn.Visible = true;
            this.txtAddDate.Text = "自动生成,无需填写";
            this.txtAddDate.Enabled = false;
            this.message.Msg = "友情链接（新增）";
            this.message.msgIco = xu_happy_you.Control.Message.msgIcos.information;
        }
    }

    /// <summary>
    /// 确定
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void imgBtn_Click(object sender, ImageClickEventArgs e)
    {
        this.GetModelFromPage();
        if ("add" == this.hfdFlag.Value)//新增
        {
            if (DB.Add(this.friendLinkEntity))
            {
                this.message.Msg = "友情链接新增成功";
                this.message.msgIco = xu_happy_you.Control.Message.msgIcos.success;
            }
            else
            {
                this.message.Msg = "友情链接新增失败";
                this.message.msgIco = xu_happy_you.Control.Message.msgIcos.error;
            }
        }
        else if ("edit" == this.hfdFlag.Value)//编辑
        {
            if (DB.Update(this.friendLinkEntity))
            {
                this.message.Msg = "友情链接编辑成功";
                this.message.msgIco = xu_happy_you.Control.Message.msgIcos.success;
            }
            else
            {
                this.message.Msg = "友情链接编辑失败";
                this.message.msgIco = xu_happy_you.Control.Message.msgIcos.error;
            }
        }
    }
}
